package com.huilian.iotbox.admin.wap.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.BrandInfoDao;
import com.huilian.iotbox.admin.wap.dao.CustomerServiceInfoDao;
import com.huilian.iotbox.admin.wap.service.CustomerServiceInfoService;
import com.huilian.iotbox.admin.wap.service.MerchantStoreService;
import com.huilian.iotbox.admin.wap.service.RabbitSenderService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.BrandInfoDto;
import com.huilian.iotbox.data.dto.CustomerServiceInfoDto;
import com.huilian.iotbox.data.dto.DiscountPackageDto;
import com.huilian.iotbox.data.dto.MerchantStoreDto;
import com.huilian.iotbox.data.po.MerchantStore;
import com.huilian.iotbox.data.properties.ImageProperties;
import com.huilian.iotbox.data.service.UploadService;
import com.huilian.iotbox.data.vo.CustomerServiceInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-11-09 16:10
 * @Description:
 */
@Service
public class CustomerServiceInfoServiceImpl implements CustomerServiceInfoService {

    @Autowired
    private CustomerServiceInfoDao customerServiceInfoDao;
    @Autowired
    private BrandInfoDao brandInfoDao;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private ImageProperties imageProperties;
    @Autowired
    private RabbitSenderService rabbitSenderService;
    @Autowired
    private MerchantStoreService merchantStoreService;

    @Override
    public PageInfo findByUserIdPages(CustomerServiceInfoDto customerServiceInfoDto) {
        PageHelper.startPage(customerServiceInfoDto.getPageNum(), customerServiceInfoDto.getPageSize());
        List<CustomerServiceInfoVo> list = customerServiceInfoDao.findByUserIdList(customerServiceInfoDto);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<CustomerServiceInfoVo> findByUserIdList(CustomerServiceInfoDto customerServiceInfoDto) {
        return customerServiceInfoDao.findByUserIdList(customerServiceInfoDto);
    }

    @Override
    public CustomerServiceInfoVo findByMerchantStoreId(CustomerServiceInfoDto customerServiceInfoDto) {
        return customerServiceInfoDao.findByMerchantStoreId(customerServiceInfoDto);
    }

    /**
     * 客服微信编辑
     *
     * @param customerServiceInfoDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse editWeChat(CustomerServiceInfoDto customerServiceInfoDto) {
        if (StringUtils.isEmpty(customerServiceInfoDto.getServiceWeChatNumber()) && StringUtils.isEmpty(customerServiceInfoDto.getServiceWeChatQr())) {
            return ServerResponse.createByError("客服微信号和客服微信二维码必须填写或选择至少一个");
        }
        if (customerServiceInfoDto.getStoreIds().size() <= 0) {
            BrandInfoDto brandInfoDto = new BrandInfoDto();
            brandInfoDto.setId(customerServiceInfoDto.getBrandInfoId());
            brandInfoDto.setServiceName(customerServiceInfoDto.getServiceName());
            brandInfoDto.setServiceWeChatNumber(customerServiceInfoDto.getServiceWeChatNumber());
            brandInfoDto.setServiceWeChatQr(customerServiceInfoDto.getServiceWeChatQr());
            brandInfoDto.setUserId(customerServiceInfoDto.getUserInfo().getId());
            brandInfoDao.updateWeChat(brandInfoDto);
            return ServerResponse.createBySuccess();
        }
        Set<String> serviceWeChatQrs = new HashSet();
        for (Integer storeId : customerServiceInfoDto.getStoreIds()) {
            customerServiceInfoDto.setMerchantStoreId(storeId);
            CustomerServiceInfoVo customerServiceInfoVo = this.findByMerchantStoreId(customerServiceInfoDto);
            // 如果是空的就插入一条数据
            if (ObjectUtils.isEmpty(customerServiceInfoVo)) {
                customerServiceInfoDao.insert(customerServiceInfoDto);
            } else {
                // 把需要删除的图片名称存到集合
                serviceWeChatQrs.add(customerServiceInfoVo.getServiceWeChatQr());
                customerServiceInfoDao.updateWeChat(customerServiceInfoDto);
            }
        }
        // 如果是修改的就判断是否是批量修改,然后把每一张图片都删除
        if (customerServiceInfoDto.getStoreIds().size() > 1) {
            // 避免数据库修改失败后图片删除了,把循环写在sql语句执行后
            for (String serviceWeChatQr : serviceWeChatQrs) {
                System.err.println(imageProperties.getBaseUrl() + serviceWeChatQr);
                uploadService.deleteByFileNameToFTP(serviceWeChatQr);
            }
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteServiceWeChatQr(CustomerServiceInfoDto customerServiceInfoDto) {
        int result = customerServiceInfoDao.findByServiceWeChatQr(customerServiceInfoDto);
        if (result <= 0) {
            return uploadService.deleteByFileNameToFTP(customerServiceInfoDto.getServiceWeChatQr());
        }
        return ServerResponse.createBySuccess();
    }

    /**
     * 客服电话号码编辑
     *
     * @param customerServiceInfoDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse editPhone(CustomerServiceInfoDto customerServiceInfoDto) {
        if (StringUtils.isEmpty(customerServiceInfoDto.getServiceMobilePhone()) && StringUtils.isEmpty(customerServiceInfoDto.getServiceFixedTelephone())) {
            return ServerResponse.createByError("请填写客服电话");
        }
        if (!StringUtils.isEmpty(customerServiceInfoDto.getServiceMobilePhone()) && !StringUtils.isEmpty(customerServiceInfoDto.getServiceFixedTelephone())) {
            return ServerResponse.createByError("请求出错,请重新访问");
        }
        if (customerServiceInfoDto.getStoreIds().size() <= 0) {
            BrandInfoDto brandInfoDto = new BrandInfoDto();
            brandInfoDto.setId(customerServiceInfoDto.getBrandInfoId());
            brandInfoDto.setUserId(customerServiceInfoDto.getUserInfo().getId());
            brandInfoDto.setServiceMobilePhone(customerServiceInfoDto.getServiceMobilePhone());
            brandInfoDto.setServiceFixedTelephone(customerServiceInfoDto.getServiceFixedTelephone());
            brandInfoDao.updatePhone(brandInfoDto);

            // 超级兑币机才需要发 信息变动 信号
            MerchantStoreDto merchantStoreDto = new MerchantStoreDto();
            merchantStoreDto.setUserInfo(customerServiceInfoDto.getUserInfo());
            List<MerchantStore> merchantStoreList = merchantStoreService.findAllMerchantStoreByMchId(merchantStoreDto);
            if (merchantStoreList.size() > 0) {
                for (MerchantStore merchantStore : merchantStoreList) {
                    DiscountPackageDto condition = new DiscountPackageDto();
                    condition.setMerchantStoreId(merchantStore.getId());
                    rabbitSenderService.sendDiscountPackageChange(condition);
                }
            }
            return ServerResponse.createBySuccess();
        }
        for (Integer storeId : customerServiceInfoDto.getStoreIds()) {
            customerServiceInfoDto.setMerchantStoreId(storeId);
            CustomerServiceInfoVo customerServiceInfoVo = this.findByMerchantStoreId(customerServiceInfoDto);
            // 如果是空的就插入一条数据
            if (ObjectUtils.isEmpty(customerServiceInfoVo)) {
                customerServiceInfoDao.insert(customerServiceInfoDto);
            } else {
                customerServiceInfoDao.updatePhone(customerServiceInfoDto);
            }

            // 超级兑币机才需要发 信息变动 信号
            DiscountPackageDto condition = new DiscountPackageDto();
            condition.setMerchantStoreId(storeId);
            rabbitSenderService.sendDiscountPackageChange(condition);
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse clearPhone(CustomerServiceInfoDto customerServiceInfoDto) {
        Integer result = 0;
        if (customerServiceInfoDto.getStoreIds().size() <= 0) {
            BrandInfoDto brandInfoDto = new BrandInfoDto();
            brandInfoDto.setId(customerServiceInfoDto.getBrandInfoId());
            brandInfoDto.setUserId(customerServiceInfoDto.getUserInfo().getId());
            brandInfoDto.setServiceMobilePhone(customerServiceInfoDto.getServiceMobilePhone());
            brandInfoDto.setServiceFixedTelephone(customerServiceInfoDto.getServiceFixedTelephone());
            result = brandInfoDao.updatePhone(brandInfoDto);
        } else {
            result = customerServiceInfoDao.updatePhone(customerServiceInfoDto);
        }
        return ServerResponse.createBySuccess(result);
    }

    @Override
    public ServerResponse clearWeChat(CustomerServiceInfoDto customerServiceInfoDto) {
        Integer result = 0;
        if (customerServiceInfoDto.getStoreIds().size() <= 0) {
            BrandInfoDto brandInfoDto = new BrandInfoDto();
            brandInfoDto.setId(customerServiceInfoDto.getBrandInfoId());
            brandInfoDto.setServiceName(customerServiceInfoDto.getServiceName());
            brandInfoDto.setServiceWeChatNumber(customerServiceInfoDto.getServiceWeChatNumber());
            brandInfoDto.setServiceWeChatQr(customerServiceInfoDto.getServiceWeChatQr());
            brandInfoDto.setUserId(customerServiceInfoDto.getUserInfo().getId());
            result = brandInfoDao.updateWeChat(brandInfoDto);
        } else {
            result = customerServiceInfoDao.updateWeChat(customerServiceInfoDto);
        }
        return ServerResponse.createBySuccess(result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse editWeChatByApplet(CustomerServiceInfoDto customerServiceInfoDto) {
        if (StringUtils.isEmpty(customerServiceInfoDto.getServiceWeChatNumber()) && StringUtils.isEmpty(customerServiceInfoDto.getServiceWeChatQr())) {
            return ServerResponse.createByError("客服微信号和客服微信二维码必须填写或选择至少一个");
        }
        if (!org.springframework.util.StringUtils.isEmpty(customerServiceInfoDto.getBrandInfoId())) {
            BrandInfoDto brandInfoDto = new BrandInfoDto();
            brandInfoDto.setId(customerServiceInfoDto.getBrandInfoId());
            brandInfoDto.setServiceName(customerServiceInfoDto.getServiceName());
            brandInfoDto.setServiceWeChatNumber(customerServiceInfoDto.getServiceWeChatNumber());
            brandInfoDto.setServiceWeChatQr(customerServiceInfoDto.getServiceWeChatQr());
            brandInfoDto.setUserId(customerServiceInfoDto.getUserInfo().getId());
            brandInfoDao.updateWeChat(brandInfoDto);
        }

        Set<String> serviceWeChatQrs = new HashSet();
        for (Integer storeId : customerServiceInfoDto.getStoreIds()) {
            customerServiceInfoDto.setMerchantStoreId(storeId);
            CustomerServiceInfoVo customerServiceInfoVo = this.findByMerchantStoreId(customerServiceInfoDto);
            // 如果是空的就插入一条数据
            if (ObjectUtils.isEmpty(customerServiceInfoVo)) {
                customerServiceInfoDao.insert(customerServiceInfoDto);
            } else {
                // 把需要删除的图片名称存到集合
                serviceWeChatQrs.add(customerServiceInfoVo.getServiceWeChatQr());
                customerServiceInfoDao.updateWeChat(customerServiceInfoDto);
            }
        }
        // 如果是修改的就判断是否是批量修改,然后把每一张图片都删除
        if (customerServiceInfoDto.getStoreIds().size() > 1) {
            // 避免数据库修改失败后图片删除了,把循环写在sql语句执行后
            for (String serviceWeChatQr : serviceWeChatQrs) {
                System.err.println(imageProperties.getBaseUrl() + serviceWeChatQr);
                uploadService.deleteByFileNameToFTP(serviceWeChatQr);
            }
        }
        return ServerResponse.createBySuccess();
    }
}
